<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>添加车辆</title>
    <script type="text/javascript" src="/admin/qrcode/jq.js"></script>
    <script type="text/javascript" src="/admin/qrcode/bot.js"></script>
    <link rel="stylesheet" type="text/css" href="/admin/css/boot.css"></link>
    <link rel="stylesheet" type="text/css" href="/admin/css/sl.css"></link>
    <style>
        label span {
            color: #ff0000;
            padding-right: 5px;
        }
    </style>
</head>

<body>
<h2>添加车辆</h2>
<form id="myForm" class="form-horizontal" method="post" action="">
    <div class="form-group">
        <label class="col-sm-1 control-label">品牌名称</label>
        <div class="col-sm-2">
            <select name="firm" id="firm" class="form-control" style="width: 160px">
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">车辆名称</label>
        <div class="col-sm-2">
            <select name="carId" id="carId" class="form-control" style="width: 160px">
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label">车辆类型</label>
        <div class="col-sm-2">
            <select name="autoId" id="autoId" class="form-control" style="width: 160px">
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-1 control-label"></label>
        <div class="col-sm-9">
            <button id="btnAdd" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
            <button type="reset" class="btn btn-sm btn-warning">重&nbsp;置</button>
        </div>
    </div>
</form>
</body>
</html>
<script>
    $(function () {
        $.ajax({
            type: "get",
            url: "/Spencer/queryByFirm",
            data: "",
            dataType: "json",
            success: function (vo) {
                let list = vo;
                //alert(list);
                let str = '<option value="-1">请选择车辆品牌</option>';
                for (let i = 0; i < list.length; i++) {
                    let obj = list[i];
                    str += '<option value="' + obj + '">' + obj + '</option>'
                    $("#firm").html(str);
                }
            }
        })

        $("#firm").change(function () {
            let val = $("#firm").val()
            $.ajax({
                type: "POST",
                url: "/Spencer/queryByAutoId",
                data: {firm: val},
                dataType: "json",
                success: function (vo) {
                    let list = vo;
                    //alert(list);
                    let str = '<option value="-1">请选择车系</option>';
                    for (let i = 0; i < list.length; i++) {
                        let obj = list[i];
                        str += '<option value="' + obj.id + '">' + obj.carName + '</option>'
                        $("#carId").html(str);
                    }
                }
            })
        })

        $("#carId").change(function () {
            let id = $("#carId").val()
            $.ajax({
                type: "get",
                url: "/Spencer/queryByModel",
                data: {id: id},
                dataType: "json",
                success: function (vo) {
                    let list = vo;
                    //alert(list);
                    let str = '<option value="-1">请选择车型</option>';
                    for (let i = 0; i < list.length; i++) {
                        if (list[i].auto.used == 0) {
                            let obj = list[i];
                            str += '<option value="' + obj.auto.autoId + '">' + obj.auto.model + '</option>'
                            $("#autoId").html(str);
                        }
                    }
                }
            })
        })

        //提交按钮的单击事件
        $("#btnAdd").click(function () {
            $.ajax({
                type: "POST",
                url: "/Spencer/addRelevancy",
                data: $("#myForm").serialize(),
                dataType: "json",
                success: function (vo) {
                    if (vo.code == 200) {
                        window.location.href = "list.html";
                    } else {
                        alert("添加失败！" + vo.msg);
                    }
                }
            });
        });
    });
</script>